package test5.lupeng.jf.com.teamdemo.activitys;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.adapter.BlogAdapter;
import test5.lupeng.jf.com.teamdemo.bean.request.HotBlogBean;
import test5.lupeng.jf.com.teamdemo.bean.response.BlogResponse;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

public class BlogClassifyActivity extends AppCompatActivity {
    ArrayList<BlogResponse.ResultBean> mHotBlogResultBeen=new ArrayList<>();
    private TextView tv_toolbar;
    private BlogAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_classify);
        int type= getIntent().getIntExtra("分类",0);
        initView();

        //判断数据来源
        switch(type){
            case  1:
                tv_toolbar.setText("杂谈");
                break;
            case 2:
                tv_toolbar.setText("提问");
                break;
            case 3:
                tv_toolbar.setText("分享");
                break;
        }
        getData("",type,10,0);

    }

    /**
     * 初始化视图
     */
    private void initView(){
        tv_toolbar = (TextView) findViewById(R.id.tv_toolbar);
        RecyclerView rl_blogclassify= (RecyclerView) findViewById(R.id.rl_blogclassify);
        rl_blogclassify.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BlogAdapter(mHotBlogResultBeen);
        rl_blogclassify.setAdapter(mAdapter);
     ImageView iv_back= (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /**
     * 获取网络数据
     * @param userId       用户id
     * @param blogType     博客类型
     * @param limit        个数
     * @param timestamp    时间戳
     */
    private  void getData(String userId, int blogType,int limit , int timestamp ){
        HotBlogBean bean  = new HotBlogBean(userId,blogType,limit,timestamp);
        String url  = "https://leancloud.cn:443/1.1/functions/getBlogList";
        OkHttpClient client  = new OkHttpClient();
        Request request= HTTPUtils.requestPost(bean,url);
        client. newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code()==200){
                    String result = response.body().string();
                    //Log.e(TAG, "onResponse: "+result );
                    BlogResponse blogResponse = new Gson().fromJson(result,BlogResponse.class);
                    mHotBlogResultBeen.clear();
                    mHotBlogResultBeen.addAll(blogResponse.getResult());
                   runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.notifyDataSetChanged();
                        }
                    });
                }
            }
        });

    }

}
